﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.XH.FormPlugIn
{
    [Description("茂名新辉销售订单表单插件"), HotUpdate]
    public class SalPlugln : AbstractBillPlugIn
    {
        //public override void AfterBindData(EventArgs e)
        //{
        //    base.AfterBindData(e);
        //    var entityKey = "FSaleOrderEntry";
        //    var backColor = "#ead1dc";
        //    GridSetRowBackcolor(this.View, entityKey, backColor, 1);
        //}

        private void GridSetRowBackcolor(IBillView view, string entityKey, string color, int rowIndex)
        {
            var value = new JSONObject();
            value[rowIndex.ToString()] = color;
            var jo = new JSONObject();
            jo["backcolor"] = value;
            var grid = view.GetControl<EntryGrid>(entityKey);
            var formState = view.GetService<IDynamicFormState>();
            formState.AftInvokeControlMethod(grid.ControlAppearance, "UpdateFieldStates", jo);
        }

        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if (e.Key == "FCustId")
            {
                long custId = Convert.ToInt64(e.NewValue);
                string strDate = DateTime.Now.ToString("yyyy-MM-dd");
                string endDate = DateTime.Now.ToString("yyyy-MM-dd");
                decimal allDatas = GetDatas(custId, strDate, endDate);
                if (allDatas != 0)
                {
                    this.Model.SetValue("FDQZQK", allDatas, 0);
                }
            }
        }

        private decimal GetDatas(long custId, string strDate, string endDate)
        {
            string sql = string.Format($@"/*dialect*/ 
            SELECT
                ISNULL(A1.FQCYS, 0) - ISNULL(A2.FQCSK, 0) AS FQCYE
            FROM (
                SELECT 
                    T0.FCUSTOMERID AS FCUSTID,
                    SUM(T1.FALLAMOUNT) AS FQCYS
                FROM t_AR_receivable T0
                JOIN t_AR_receivableEntry T1 ON T1.FID = T0.FID
                WHERE T0.FCUSTOMERID={custId}
                GROUP BY T0.FCUSTOMERID
            ) A1
            FULL JOIN (
                SELECT 
                    T0.FCONTACTUNIT AS FCUSTID,
                    SUM(T1.FREALRECAMOUNT) AS FQCSK
                FROM T_AR_RECEIVEBILL T0
                JOIN T_AR_RECEIVEBILLENTRY T1 ON T1.FID = T0.FID
                WHERE T0.FCONTACTUNITTYPE = 'BD_Customer'
                    AND T0.FCONTACTUNIT={custId}
                GROUP BY T0.FCONTACTUNIT
            ) A2 ON A2.FCUSTID = A1.FCUSTID ");
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(this.Context, sql);
            if (doc.Count > 0)
            {
                return Convert.ToDecimal(doc[0][0]);
            }
            else
            {
                return 0;
            }
        }

        public override void ButtonClick(ButtonClickEventArgs e)
        {
            base.ButtonClick(e);
            if (e.Key.Equals("FKHDZ"))
            {
                DynamicObject cust = this.Model.DataObject["CustId"] as DynamicObject;
                string custId = cust == null ? "0" : cust["id"].ToString();
                DynamicObject org = this.Model.DataObject["SaleOrgId"] as DynamicObject;
                string orgId = org == null ? "0" : org["id"].ToString();
                BillShowParameter showParam = new BillShowParameter();
                showParam.FormId = "POSW_KHWLZZB";
                //showParam.PKey = "100462";
                //传递自定义参数
                showParam.CustomParams.Add("CustId", custId);
                showParam.CustomParams.Add("OrgId", orgId);
                if (custId != "0")
                {
                    this.View.ShowForm(showParam);
                }
                else
                {
                    this.View.ShowErrMessage("客户为空，请先选择客户！");
                    return;
                }
            }
        }
    }
}
